package action;

import entity.DlbEntity;
import entity.XsbEntity;
import org.apache.struts2.ServletActionContext;
import service.DlbService;
import service.XsbService;
import utils.ResultUtils;
import utils.Singleton;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Lei
 * @create 2021-04-16 16:41
 */
public class XsbAction {

    private String img;

    private String newPassword;

    private String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    private Integer page = 1;
    private Integer limit = 10;

    private String xh = "";
    private String xm = "";

    public String getXh() {
        return xh;
    }

    public void setXh(String xh) {
        this.xh = xh;
    }

    public String getXm() {
        return xm;
    }

    public void setXm(String xm) {
        this.xm = xm;
    }

    private short xb;
    private Timestamp cssj;
    private int zyId;
    private Integer zxf;
    private String bz;

    public short getXb() {
        return xb;
    }

    public void setXb(short xb) {
        this.xb = xb;
    }

    public Timestamp getCssj() {
        return cssj;
    }

    public void setCssj(Timestamp cssj) {
        this.cssj = cssj;
    }

    public int getZyId() {
        return zyId;
    }

    public void setZyId(int zyId) {
        this.zyId = zyId;
    }

    public Integer getZxf() {
        return zxf;
    }

    public void setZxf(Integer zxf) {
        this.zxf = zxf;
    }

    public String getBz() {
        return bz;
    }

    public void setBz(String bz) {
        this.bz = bz;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    private XsbService xsbService = (XsbService) Singleton.GetApplicationContext().getBean("XsbServiceImpl");

    private DlbService dlbService = (DlbService) Singleton.GetApplicationContext().getBean("DlbServiceImpl");

    public String addUser() throws IOException {
        XsbEntity xsbEntity1 = xsbService.selectRecord(xh);
        if (xsbEntity1 != null) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("code", 0);
            stringObjectHashMap.put("msg", "存在这个用户了");
            ResultUtils.toJson(ServletActionContext.getResponse(), stringObjectHashMap);
            return "fail";
        }
        XsbEntity xsbEntity = new XsbEntity();
        xsbEntity.setXh(xh);
        xsbEntity.setXm(xm);
        xsbEntity.setXb(xb);
        xsbEntity.setCssj(cssj);
        xsbEntity.setZyId(zyId);
        xsbEntity.setZxf(zxf);
        xsbEntity.setBz(bz);
        int i = xsbService.addUser(xsbEntity);

        DlbEntity dlbEntity = new DlbEntity();
        dlbEntity.setXh(xh);
        dlbEntity.setKl("123456");
        dlbService.addDlbRecord(dlbEntity);
        if (i != 0) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("code", 200);
            stringObjectHashMap.put("msg", "添加成功");
            ResultUtils.toJson(ServletActionContext.getResponse(), stringObjectHashMap);
            return "success";
        }
        return "fail";
    }

    public String updatePassword() throws Exception {
        HttpServletRequest request = (HttpServletRequest) ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        XsbEntity user = (XsbEntity) session.getAttribute("user");
        DlbEntity admin = dlbService.selectByUserNameAndPassword(user.getXh(), password);
        if (admin==null){
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("code", 0);
            stringObjectHashMap.put("msg", "原密码错误");
            ResultUtils.toJson(ServletActionContext.getResponse(), stringObjectHashMap);
            return "fail";

        }
        admin.setXh(user.getXh());
        admin.setKl(newPassword);
        int updaterecord = dlbService.updaterecord(admin);
        if (updaterecord != 0) {
            //删除掉存放的数据。
            session.removeAttribute("user");
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("code", 200);
            stringObjectHashMap.put("msg", "修改成功");
            ResultUtils.toJson(ServletActionContext.getResponse(), stringObjectHashMap);
            return "success";
        }
        return "fail";


    }


    public String updateHeaderImg() throws Exception {
        XsbEntity xsbEntity = new XsbEntity();
        HttpServletRequest request = (HttpServletRequest) ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        XsbEntity user = (XsbEntity) session.getAttribute("user");
        user.setZp(img);
        int i = xsbService.updateUser(user);
        if (i != 0) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("code", 200);
            stringObjectHashMap.put("msg", "上传成功");
            ResultUtils.toJson(ServletActionContext.getResponse(), stringObjectHashMap);
            return "success";
        }
        return "fail";


    }

    public String updateUser() throws IOException {

        XsbEntity xsbEntity = new XsbEntity();
        xsbEntity.setXh(xh);
        xsbEntity.setXm(xm);
        xsbEntity.setXb(xb);
        xsbEntity.setCssj(cssj);
        xsbEntity.setZyId(zyId);
        xsbEntity.setZxf(zxf);
        xsbEntity.setBz(bz);
        //进行刷新数据
        int i = xsbService.updateUser(xsbEntity);

        if (i != 0) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("code", 200);
            stringObjectHashMap.put("msg", "修改成功");
            ResultUtils.toJson(ServletActionContext.getResponse(), stringObjectHashMap);
            return "success";
        }
        return "fail";
    }

    public String deleteUser() throws IOException {
        XsbEntity xsbEntity = new XsbEntity();
        xsbEntity.setXh(xh);
        //进行删除了
        int i = xsbService.deleteUser(xsbEntity);
        if (i != 0) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("code", 200);
            stringObjectHashMap.put("msg", "删除成功");
            ResultUtils.toJson(ServletActionContext.getResponse(), stringObjectHashMap);
            return "success";
        }
        return "fail";
    }

    /**
     * @return java.lang.String
     * @author
     * @description 得到所有的一个用户的列表转化成为pagehelper的一个操作，然后执行之后的操作
     * @date 10:15 2021/4/16
     * @params []
     */
    public String userList() throws IOException {
        List<XsbEntity> xsbEntities = xsbService.selectUserList(xh, xm);
        List<XsbEntity> res = new ArrayList<>();
        for (int i = (this.page - 1) * this.limit; i < xsbEntities.size() && i < this.page * this.limit; i++) {
            res.add(xsbEntities.get(i));
        }
        if (xsbEntities != null) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("code", 200);
            stringObjectHashMap.put("msg", "登录成功");
            stringObjectHashMap.put("data", res);
            stringObjectHashMap.put("total", xsbEntities.size());
            ResultUtils.toJson(ServletActionContext.getResponse(), stringObjectHashMap);
            return "success";
        }
        return "fail";
    }

    public String toUserList() {
        return "toUserList";
    }


    public String logoutMethod() throws Exception {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("code", 200);
        stringObjectHashMap.put("msg", "退出成功");
        HttpServletRequest request = (HttpServletRequest) ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        ResultUtils.toJson(ServletActionContext.getResponse(), stringObjectHashMap);
        return "success";
    }
}
